﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using System.Data;
using Menu = Z16.BLI.Menu;

namespace Z16.SITE
{
    public partial class ListarMenu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((Session["CodPerfil"] == null) || (Session["CodUsr"] == null))
            {
                Response.Write("<script>window.location=\'Login.aspx';</script>");
                return;
            }

            if(!IsPostBack)
            {
                int? codMenu = null;

                if (Request.QueryString["id"] != null)
                    codMenu = int.Parse(Request.QueryString["id"]);

                CarregarMenu(codMenu);
            }
        }

        private DataTable CarregarMenu(int? codMenu)
        {
            Menu menu = new Menu();
            DataTable tabela = null;

            try
            {
                tabela = menu.ListarMenusId(codMenu);
                GridView1.DataSource = tabela;
                GridView1.DataBind();
            }
            catch (Exception ee)
            {
                lblMensagem.Text = ee.Message;
            }
            finally
            {
                menu = null;
            }
            return tabela;
        }

        protected void ExcluirMenu(object sender, GridViewDeleteEventArgs e)
        {
            int MenuID = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

            Menu menu = new Menu();
            try
            {

                lblMensagem.Text = "Registro deletado com sucesso.";
                GridView1.DataSource = menu.ListarMenusId(int.Parse(Request.QueryString["id"]));
                GridView1.DataBind();
            }
            catch (Exception ee)
            {
                lblMensagem.Text = ee.Message;
            }
        }

        protected void btnNovoMenu_Click(object sender, EventArgs e)
        {

            int? codMenu = null;

            if (Request.QueryString["id"] != null)
                codMenu = int.Parse(Request.QueryString["id"]);

            Response.Redirect("addCadMenu.aspx?id=" + codMenu);
        }

        protected void btnPesquisar_Click(object sender, EventArgs e)
        {
            int? codMenu = null;

            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
                codMenu = int.Parse(Request.QueryString["id"]);

            try
            {
                Menu menu = new Menu();
                DataTable dt = menu.ListarMenus(txtMenu.Text);

                List<DataRow> list = dt.AsEnumerable().ToList();

                if (list.Count > 0)
                {
                    var dem = list.First();

                    codMenu = int.Parse(dem[0].ToString());
                }

                CarregarMenu(codMenu);
            }
            catch (Exception ee)
            {
                lblMensagem.Text = ee.Message;
            }
        }
    }
}